using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Admin;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;
public class HL_Admin_Admin_AddSkin : Admin, IRequiresSessionState
{
	protected string SkinDir = "";
	protected string CurProjectName = "";
	protected FormView FormView1;
	protected ObjectDataSource ObjectDataSource1;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.admin.CheckAdminPower(12))
		{
			base.WriteLog("View", Language.GetAdminMsg("Log/ObjectID", "0"), "Skin");
			base.WriteRightErrMsg();
		}
		TemProjectBll temProjectBll = new TemProjectBll(this.conn);
		DataTable defaultProject = temProjectBll.GetDefaultProject();
		this.SkinDir = "/" + defaultProject.Rows[0]["SkinDir"].ToString();
		this.CurProjectName = defaultProject.Rows[0]["TemplateProjectName"].ToString();
		if (!base.IsPostBack)
		{
			base.Response.Cookies["backPage"].Value = base.Request.UrlReferrer.AbsolutePath;
			base.Response.Cookies["backPage"].Expires = DateTime.Now.AddHours(2.0);
			string a = base.Request.QueryString["Action"];
			if (a == "Add")
			{
				this.FormView1.DefaultMode = FormViewMode.Insert;
				if (Fetch.Get("SkinType") != string.Empty)
				{
					DropDownList dropDownList = (DropDownList)this.FormView1.FindControl("Skin_Type");
					dropDownList.SelectedValue = Fetch.Get("SkinType");
					TextBox textBox = (TextBox)this.FormView1.FindControl("Skin_SavePath");
					textBox.Text = "/" + Fetch.Get("SkinType") + "/";
				}
			}
			else
			{
				TextBox textBox2 = (TextBox)this.FormView1.FindControl("Skin_css");
				TextBox textBox3 = (TextBox)this.FormView1.FindControl("Skin_SavePath");
				textBox2.Text = this.GetSkinContent(this.SkinDir + textBox3.Text);
				Image image = (Image)this.FormView1.FindControl("previewImgShow");
				if (image.ImageUrl.Length < 10)
				{
					image.ImageUrl = "../Images/nopicture.gif";
				}
			}
			FileUpload fileUpload = (FileUpload)this.FormView1.FindControl("FileUpload1");
			fileUpload.Attributes.Add("onchange", "return ShowImage(this)");
		}
	}
	protected void ObjectDataSource1_Init(object sender, EventArgs e)
	{
		base.Response.Cache.SetNoStore();
	}
	public void onUpdateSkin(object sender, EventArgs e)
	{
		TextBox textBox = (TextBox)this.FormView1.FindControl("SkinName");
		DropDownList dropDownList = (DropDownList)this.FormView1.FindControl("Skin_Type");
		TextBox textBox2 = (TextBox)this.FormView1.FindControl("Skin_css");
		TextBox textBox3 = (TextBox)this.FormView1.FindControl("Skin_ProjectName");
		TextBox textBox4 = (TextBox)this.FormView1.FindControl("Skin_SavePath");
		CheckBox checkBox = (CheckBox)this.FormView1.FindControl("IsDefault");
		string text = textBox.Text;
		string text2 = textBox2.Text;
		bool isDefault = false;
		if (checkBox.Checked)
		{
			isDefault = true;
		}
		string text3 = textBox3.Text;
		int skinID = int.Parse(base.Request["SkinID"]);
		string selectedValue = dropDownList.SelectedValue;
		string text4 = textBox4.Text;
		string uploadImgUrl = this.GetUploadImgUrl();
		SkinBll skinBll = new SkinBll();
		if (skinBll.UpateSkin(text, text2, isDefault, text3, skinID, selectedValue, this.SkinDir, text4, uploadImgUrl))
		{
			base.Response.Write("<script type=\"text/javascript\">alert('修改成功！');window.returnValue=true;window.close();window.opener.location.reload();</script>");
		}
		else
		{
			base.Response.Write("<script type=\"text/javascript\">alert('修改失败，可能文件没有写入权限！');window.returnValue=false;window.close();</script>");
		}
	}
	public void onAddSkin(object sender, EventArgs e)
	{
		TextBox textBox = (TextBox)this.FormView1.FindControl("SkinName");
		DropDownList dropDownList = (DropDownList)this.FormView1.FindControl("Skin_Type");
		TextBox textBox2 = (TextBox)this.FormView1.FindControl("Skin_css");
		TextBox textBox3 = (TextBox)this.FormView1.FindControl("Skin_ProjectName");
		TextBox textBox4 = (TextBox)this.FormView1.FindControl("Skin_SavePath");
		CheckBox checkBox = (CheckBox)this.FormView1.FindControl("IsDefault");
		string text = textBox.Text;
		string text2 = textBox2.Text;
		bool isDefault = false;
		if (checkBox.Checked)
		{
			isDefault = true;
		}
		string curProjectName = this.CurProjectName;
		string selectedValue = dropDownList.SelectedValue;
		string skinPath = textBox4.Text + textBox3.Text + ".css";
		string uploadImgUrl = this.GetUploadImgUrl();
		SkinBll skinBll = new SkinBll(this.conn);
		if (skinBll.AddSkin(text, text2, isDefault, true, curProjectName, selectedValue, this.SkinDir, skinPath, uploadImgUrl))
		{
			if (base.Request.Cookies["backPage"].Value != null)
			{
				base.Response.Write("<script type=\"text/javascript\">if(confirm('添加成功，需要继续添加吗？')){window.returnValue=true;window.close();window.opener.location.reload();}else{window.location.href='" + base.Request.Cookies["backPage"].Value.Substring(base.Request.Cookies["backPage"].Value.LastIndexOf('/') + 1) + "?t='+new Date().getDate();}</script>");
			}
			else
			{
				base.Response.Write("<script type=\"text/javascript\">if(confirm('添加成功，需要继续添加吗？')){window.returnValue=true;window.close();window.opener.location.reload();}</script>");
			}
		}
		else
		{
			base.Response.Write("<script type=\"text/javascript\">alert('添加失败，可能文件没有写入权限！');window.returnValue=false;window.close();</script>");
		}
	}
	protected string GetUploadImgUrl()
	{
		string result = "";
		FileUpload fileUpload = (FileUpload)this.FormView1.FindControl("FileUpload1");
		if (fileUpload.FileName.Length > 0)
		{
			DropDownList dropDownList = (DropDownList)this.FormView1.FindControl("Skin_Type");
			TextBox textBox = (TextBox)this.FormView1.FindControl("Skin_SavePath");
			string selectedValue = dropDownList.SelectedValue;
			string uploadFileName = selectedValue + "_preview_" + textBox.Text;
			Watermark watermark = new Watermark();
			string uploadFilePath = base.Server.MapPath("..").ToString() + "\\UploadFile\\Preview\\";
			string str = watermark.UploadPic(fileUpload.PostedFile, uploadFilePath, uploadFileName, false, false, 150, 203);
			result = "/UploadFile/Preview/" + str;
		}
		return result;
	}
	protected string GetSkinContent(string SkinPath)
	{
		SkinPath = "../Skin" + SkinPath;
		string result;
		try
		{
			string filePath = base.Server.MapPath(SkinPath);
			FileIO fileIO = new FileIO();
			result = fileIO.ReadFromFile(filePath);
		}
		catch (Exception ex)
		{
			result = ex.Message + "\r\n请清空风格内容后并修改风格路径保存后再进行修改!";
		}
		return result;
	}
}
